package ru.mcfr.oxygen.updater.installer;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: ws
 * Date: 28.04.11
 * Time: 18:22
 * To change this template use File | Settings | File Templates.
 */
public class JSInstaller extends AbstractInstaller{
    private ScriptEngine engine;
    public JSInstaller(String path) {
        super(path);
        ScriptEngineManager factory = new ScriptEngineManager();
        engine = factory.getEngineByName("JavaScript");
    }

    public void put(String name, Object o){
        engine.put(name, o);
    }

    @Override
    protected boolean install(String src) {
        try {

            engine.put("installser", this);

            engine.eval(new FileReader(src));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        return false;
    }
}
